package com.liujit.standard.basis.core.util;

import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.liujit.standard.basis.core.valid.ParameterInvalidItem;
import com.liujit.standard.basis.core.domain.query.PageQuery;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;

import java.util.List;

/**
 * @description: 工具类
 * @author: liujun
 * @create: 2021/3/24 7:07 下午
 **/
public class CoreUtil {

    /**
     * 获取校验异常
     *
     * @param list
     */
    public static String getMsg(List<ParameterInvalidItem> list) {
        StrBuilder res = new StrBuilder();
        for (ParameterInvalidItem item : list) {
            res.append("，" + item.getMessage());
        }
        return res.subString(1);
    }

    /**
     * 设置分页
     *
     * @param query
     */
    public static void Paging(PageQuery query) {
        PageHelper.startPage(query.getPageNumber(), query.getPageSize());
        if (StrUtil.isNotBlank(query.getSortName()) && StrUtil.isNotBlank(query.getSortOrder())) {
            PageHelper.orderBy(StrUtil.toUnderlineCase(query.getSortName()) + " " + query.getSortOrder());
        }
    }

    /**
     * 根据排序字段设置分页
     *
     * @param query
     * @param orderBy
     */
    public static void Paging(PageQuery query, String orderBy) {
        PageHelper.startPage(query.getPageNumber(), query.getPageSize());
        if (StrUtil.isNotBlank(query.getSortName()) && StrUtil.isNotBlank(query.getSortOrder())) {
            PageHelper.orderBy(StrUtil.toUnderlineCase(query.getSortName()) + " " + query.getSortOrder());
        } else {
            if (StrUtil.isNotEmpty(orderBy)) {
                PageHelper.orderBy(orderBy);
            }
        }
    }

    /**
     * 对象复制
     *
     * @return
     */
    public static ModelMapper modelMapper() {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setFullTypeMatchingRequired(true).setMatchingStrategy(MatchingStrategies.STRICT);
        return modelMapper;
    }
}
